/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.comsrv.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.comsrv.entity.CnArea;
import com.ykht.proj.comsrv.service.CnAreaService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;



/**
 * 
 *
 * @author proj code generator
 * @date 2020-11-16 17:21:47
 */
@RestController
@AllArgsConstructor
@RequestMapping("/cnarea" )
@Api(value = "cnarea", tags = "管理")
public class CnAreaController {

    private final  CnAreaService cnAreaService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param cnArea 
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnarea_view')" )
    public R getCnAreaPage(Page page, CnArea cnArea) {
        return R.ok(cnAreaService.page(page, Wrappers.query(cnArea)));
    }


	/**
	 * 获取地区地址列表
	 * @param cnArea
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/getCnAreaList" )
	public R getCnAreaList(CnArea cnArea) {
		QueryWrapper<CnArea> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(CnArea::getParentId, cnArea.getParentId())
				.eq(null != cnArea.getLevel(), CnArea::getLevel, cnArea.getLevel());
		return R.ok(cnAreaService.list(queryWrapper));
	}


	/**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnarea_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(cnAreaService.getById(id));
    }

    /**
     * 新增
     * @param cnArea 
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('comsrv_cnarea_add')" )
    public R save(@RequestBody CnArea cnArea) {
        return R.ok(cnAreaService.save(cnArea));
    }

    /**
     * 修改
     * @param cnArea 
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('comsrv_cnarea_edit')" )
    public R updateById(@RequestBody CnArea cnArea) {
        return R.ok(cnAreaService.updateById(cnArea));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('comsrv_cnarea_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(cnAreaService.removeById(id));
    }



	/**
	 * 通过id查询 地址
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询 地址", notes = "通过id查询 地址")
	@GetMapping("getByAreaId/{id}" )
	public CnArea getByAreaId(@PathVariable Integer id) {
		return cnAreaService.getById(id);
	}


}
